package com.dora.basic.poi;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExportXSSFColor {

    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\hanchen\\Desktop\\test.xlsx");
        if (file.exists()) {
            file.delete();
        }
        BufferedOutputStream out = null;
        try {
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\hanchen\\Desktop\\test.xlsx");
            out = new BufferedOutputStream(fileOutputStream);
            exportExcel(out);
        } finally {
            out.close();
        }
    }

    /**
     * @param out
     * @throws IOException
     */
    private static void exportExcel(BufferedOutputStream out) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet();
        sheet.setColumnWidth(0, 20*256);
        Row row = sheet.createRow((short) 0);
        Cell cell = row.createCell((short) 0);
        cell.setCellValue("调色板");

        // 创建一个单元格样式
        XSSFCellStyle style = workbook.createCellStyle();
        cell.setCellStyle(style);

        /*****************************使用默认颜色**************************************************/
        // 填充色
//      style.setFillForegroundColor(IndexedColors.BLUE.getIndex());
        style.setFillForegroundColor(HSSFColorPredefined.BLUE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        // 字体颜色
        Font font = workbook.createFont();
        font.setColor(IndexedColors.WHITE.getIndex());
        style.setFont(font);
        /**************************************************************************************/

        /*****************************自定义颜色**************************************************/
        XSSFColor color = new XSSFColor(new java.awt.Color(255, 0, 0));
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        XSSFColor color1 = new XSSFColor(new java.awt.Color(0, 0, 0));
        // 字体颜色
        XSSFFont font1 = workbook.createFont();
        font1.setColor(color1);
        style.setFont(font1);
        /**************************************************************************************/

        workbook.write(out);
    }
}
